package com.zhongkai.web.control.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.config.User;
import com.zhongkai.service.config.ButtonService;
import com.zhongkai.service.config.DepartmentService;
import com.zhongkai.service.config.MenuService;
import com.zhongkai.service.config.UserService;
import com.zhongkai.tools.CheckWord;

@Controller
@RequestMapping(value="/system/ajax/department.do")
public class DepartmentAjaxControl {
	
	private Logger log=Logger.getLogger(this.getClass());
	private DepartmentService departmentService;
	
	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	@Resource
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	/*根据名字找部门*/
	@SuppressWarnings("unchecked")
	@RequestMapping(params="method=findByDepartmentName")
	public String findByDepartmentName(HttpServletResponse response,String departmentName) throws IOException{
		try {
			List<User> deparmentlist = departmentService.findByHql("from Department where departmentName=?", new Object[]{departmentName});
			PrintWriter out=response.getWriter();
			if(departmentName==null||"".equals(departmentName)){
				
				out.write("<font color=\"red\">部门名称不能为空!</font>");
				return null;
			}
			
			if(deparmentlist==null||deparmentlist.size()==0||deparmentlist.isEmpty()){
				out.write("<font color=\"green\">部门名称可用!</font>");
				return null;
			
			}else{
				out.write("<font color=\"red\">该部门名称已存在!</font>");
				return null;
			}	
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return null;
		}
		
	}
	
	
	@RequestMapping(params="method=findIdentifier")
	public String findIdentifier(HttpServletResponse response,String departmentIdentifier) throws IOException{
		try {
			List<User> departmentList = departmentService.findByHql("from Department where departmentIdentifier=?", new Object[]{departmentIdentifier});
			PrintWriter out=response.getWriter();
			if(departmentIdentifier==null||"".equals(departmentIdentifier)){
				out.write("<span style='color:red'>编号不能为空!</span>");
				return null;
			}
			Pattern pattern = Pattern.compile("^\\d{1,11}$");
			if(!pattern.matcher(departmentIdentifier).matches()){
				out.write("<span style='color:red'>编号过长或非法!</span>");
				return null;
			}
			if(departmentList==null||departmentList.size()==0||departmentList.isEmpty()){
				out.write("<span style='color:green'>该编号可用!</span>");
				return null;
			
			}else{
				out.write("<span style='color:red'>该编号已存在!</span>");
				return null;
			}		
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return null;
		}
		
	}
	
}
